package com.mooltik.content;

import android.content.*;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NotesContentProvider extends ContentProvider {

    private static final String TAG = "NotesContentProvider";

    private List<HashMap<String, String>> notes = new ArrayList<HashMap<String, String>>();
    private static final String mimeType = "com.mooltik.cursor.item/note";
    private MatrixCursor cursor;
    private static final String[] columns = new String[]{"_id", "title", "text", "updated"};

    private static final String PREFS_NAME = "notes";
    private SharedPreferences settings;
    private Object savedInstanceState;
    private DatabaseHelper dbHelper;
    private static final String NOTES_TABLE_NAME = "notes";

    @Override
    public boolean onCreate() {

        dbHelper = new DatabaseHelper(getContext());
        return true;

    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(NOTES_TABLE_NAME);

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return qb.query(db, projection, selection, selectionArgs, null, null, null);

    }

    @Override
    public String getType(Uri uri) {
        return mimeType;
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {

        SQLiteDatabase db = dbHelper.getWritableDatabase();

        long rowId = db.insert(NOTES_TABLE_NAME, Notes.NOTE, contentValues);

        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(Notes.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        return db.delete(NOTES_TABLE_NAME, where, whereArgs);
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        return db.update(NOTES_TABLE_NAME, values, where, whereArgs);

    }


    private static class DatabaseHelper extends SQLiteOpenHelper {

        private static String DATABASE_NAME = "notes";
        private static int DATABASE_VERSION = 1;


        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + NOTES_TABLE_NAME + " ("
                    + Notes._ID + " INTEGER PRIMARY KEY,"
                    + Notes.TITLE + " TEXT,"
                    + Notes.NOTE + " TEXT,"
                    + Notes.CREATED_DATE + " INTEGER,"
                    + Notes.MODIFIED_DATE + " INTEGER"
                    + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + NOTES_TABLE_NAME);
            onCreate(db);
        }
    }

}
